package com.yun.platform.config;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.platform.config.dto.RData;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hanson
 * curl  基类
 * @param <T>
 */
@Slf4j
public abstract class BaseCURDController<T extends Object> {

    @Autowired
    protected HttpServletRequest request;

    public abstract ServiceImpl getService();

    @ApiOperation("保存接口")
    @PostMapping("/save")
    public RData save(@RequestBody T en){
        return RData.success(getService().save(en));
    }

    @ApiOperation("修改接口")
    @PostMapping("/update")
    public RData update(@RequestBody T en){
        return RData.success(getService().saveOrUpdate(en));
    }

    @ApiOperation("id查询接口")
    @GetMapping("/view/{id}")
    public Object findbyId(@PathVariable("id")Integer id){
        return getService().getById(id);
    }

    @ApiOperation("id删除接口")
    @GetMapping("/delete/{id}")
    public RData delete(@PathVariable("id")Integer id){
        return RData.success(getService().removeById(id));
    }


}
